<?php


namespace app\common\model;


use plug\SUtil;
use think\Db;

class ApplyReg extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_applyreg';
    // 主键ID
    protected $pk = 'applyreg_id';



    //新增、编辑(后台操作)
    function edit($params, $id=0,$ischeck = false){
        if($id>0){
            // 更新条件
            $where = ['applyreg_id' => $id];
            if($ischeck == true){
                $this->startTrans();
            }
            $rs = $this->allowField(true)->save($params, $where);
//            $rs = true;
        }else{
            $params['add_time'] = time();
            $params['apply_ip'] = SUtil::getIp();
            $params['pass_random'] = SUtil::random(6,0);
            $params['user_pass'] = SUtil::getHashBackend($params['user_pass'], $params['pass_random']);
            // 更新数据
            $this->allowField(true)->save($params);
            $rs = $this->applyreg_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        if($ischeck == true){
            $result = $this->addadminandshop($id);
            if($result === false){
                $this->rollback();
                return false;
            }
            $this->commit();
        }
        return $rs;
    }


    // 查询列表
    function get_list($cond, $orderby, $item, $limit)
    {
        // 条件
        $where = null;
        if(!empty($cond['shopname'])){
            $where[] = ['a.shopname','like',"%".$cond['shopname']."%"];
        }

        $rs = Db::table($this->table)
            ->alias('a')
            ->leftJoin('ddt_area b','a.province_code = b.id')
            ->leftJoin('ddt_area c','a.city_code = c.id')
            ->leftJoin('ddt_area d','a.area_code = d.id')
            ->leftJoin('ddt_area f','a.street_code = f.id')
            ->where($where)
            ->field($item)
            ->order($orderby)
            ->paginate($limit);
        return $rs;
    }

    // 处理添加管理用户和商铺
    public function addadminandshop($applyid)
    {
        $AreaModel = new Area();

        $data = $this->where('applyreg_id','=',$applyid)->find()->toArray();
        $is_wholesale = 1;
        switch(true){
            case $data['apply_type'] == 1:
                $groupid = 9;
                if($data['is_wholesale'] != 1){
                    $is_wholesale = 2;
                    $groupid = 12;
                }
                $shop_type = 1;
                break;
            case $data['apply_type'] == 2:
                $groupid = 10;
                $shop_type = 2;
                break;
        }
        $shopModel = new Shops();
//        $shop = $shopModel->where('shopphone','=',$data['telphone'])->find();
        $shop = $shopModel->getOne(array('shop_phone'=>$data['telphone']),'*');
        if(!empty($shop)){
            $this->setError(-1,'已存在该手机号注册商家');
            return false;
        }
        // 插入商铺
        $shopparams = [
            'shop_name' => $data['shopname'],
            'shop_phone' => $data['telphone'],
            'province_id' => $data['province_code'],
            'city_id' => $data['city_code'],
            'area_id' => $data['area_code'],
            'stree_id' => $data['street_code'],
            'address' => $data['address'],
            'licenseimg' => $data['licenseimg'],
            'imagesurl' => $data['imagesurl'],
            'shop_manager' => $data['shop_manager'],
            'shop_type' => $shop_type,
            'remark' => $data['remark'],
            'lon' => $data['lon'],
            'lat' => $data['lat'],
            'add_time' => time(),
            'is_wholesale' => $is_wholesale,
        ];

        $shopparams['province'] = $AreaModel->getOne(['id' => $data['province_code']],'name')['name'];
        $shopparams['city'] = $AreaModel->getOne(['id' => $data['city_code']],'name')['name'];
        $shopparams['area'] = $AreaModel->getOne(['id' => $data['area_code']],'name')['name'];
        $shopparams['street'] = $AreaModel->getOne(['id' => $data['street_code']],'name')['name'];
        $result1 = $shopModel->edit($shopparams);
        if($result1 === false){
            $this->setError("-1",$shopModel->getError());
            return false;
        }
        $shopid = $result1;

        $adminModel = new Admin();
        $admin = $adminModel->where('phone','=',$data['telphone'])->find();
        if(!empty($admin)){
            $this->setError(-1,'已存在该管理员账号');
            return false;
        }
        // 插入管理员
        $adminparams = [
            'user_name' => $data['telphone'],
            'phone' => $data['telphone'],
            'real_name' => $data['shop_manager'],
            'user_pass' => $data['user_pass'],
            'pass_random' => $data['pass_random'],
            'group_id' => $groupid,
            'shop_id' => $shopid,
            'add_time' => time(),
        ];

        $result2 = $adminModel->edit($adminparams);
        if($result2 === false){
            $this->setError("-1",$adminModel->getError());
            return false;
        }
        return true;
    }


    /**
     * 更新用户登录成功的信息
     */
    public function update_login($userid,$user_pass=''){
        $adminModel = new Admin();

        if(empty($userid)){
            return false;
        }
        $where = ['user_id' => $userid];

        $loginSucInfo = array(
            'last_time'=>time(),
            'last_ip'=>SUtil::getIp()
        );
        if($user_pass){
            $loginSucInfo['pass_random'] = SUtil::random(6,0);
            $loginSucInfo['user_pass'] = SUtil::getHashBackend($user_pass, $loginSucInfo['pass_random']);
        }
        $re = $adminModel->getUpdate($where, $loginSucInfo);
        if($re==false||!$loginSucInfo['pass_random']){//未更新成功或者未更新pass_random那么返回原来的sn
            $one = $adminModel->getOne($where,'pass_random');
            $loginSucInfo['pass_random'] = $one['pass_random'];
        }
        return $loginSucInfo['pass_random'];
    }




}